#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Solution
{
public:
    string fractionAddition(string expression)
    {
        // 分离分子分母
        auto splitFrac = [=](string f) -> vector<int>
        {
            int pos = f.find_first_of("/");
            int a, b;
            if (pos == string::npos)
            {
                a = stoi(f), b = 1;
            }
            else
            {
                a = stoi(f.substr(0, pos)), b = stoi(f.substr(pos + 1));
            }

            return {a, b};
        };
        // 简化分数
        auto simpleFrac = [=](string frac) -> string
        {
            vector<int> res = splitFrac(frac);
            int a = res[0], b = res[1];
            int gcd = abs(__gcd(a, b));
            return gcd != 1 ? (to_string(a / gcd) + "/" + to_string(b / gcd)) : frac;
        };
        // 分数加减法
        auto addFrac = [=](string f1, string f2) -> string
        {
            vector<int> res1 = splitFrac(f1), res2 = splitFrac(f2);
            int a1 = res1[0], b1 = res1[1], a2 = res2[0], b2 = res2[1];
            return to_string(a1 * b2 + a2 * b1) + "/" + to_string(b1 * b2);
        };
        //队列存储nums,ops默认全为+
        queue<string> numsQueue;
        string frec1 = "";
        for (size_t i = 0; i < expression.length(); i++)
        {
            // case2 0-1/2-2/3
            if (expression[i] == '-' || expression[i] == '+')
            {
                // case1 -1/2+2/3
                if (frec1 != "")
                {
                    numsQueue.push(frec1);
                }
                frec1 = expression[i] == '-' ? "-" : "";
            }
            else
            {
                frec1 += expression[i];
            }
        }
        if ("" != frec1)
        {
            numsQueue.push(frec1);
        }

        // 计算
        while (1 != numsQueue.size())
        {
            string s1 = numsQueue.front();
            numsQueue.pop();
            string s2 = numsQueue.front();
            numsQueue.pop();
            numsQueue.push(addFrac(s1, s2));
        }

        return simpleFrac(numsQueue.front());
    }

    string fractionAddition_2(string expression)
    {
        long long denominator = 0, numerator = 1; // 分子，分母
        int index = 0, n = expression.size();
        while (index < n)
        {
            // 读取分子
            long long denominator1 = 0, sign = 1;
            if (expression[index] == '-' || expression[index] == '+')
            {
                sign = expression[index] == '-' ? -1 : 1;
                index++;
            }
            while (index < n && isdigit(expression[index]))
            {
                denominator1 = denominator1 * 10 + expression[index] - '0';
                index++;
            }
            denominator1 = sign * denominator1;
            index++;

            // 读取分母
            long long numerator1 = 0;
            while (index < n && isdigit(expression[index]))
            {
                numerator1 = numerator1 * 10 + expression[index] - '0';
                index++;
            }

            denominator = denominator * numerator1 + denominator1 * numerator;
            numerator *= numerator1;
        }
        if (denominator == 0)
        {
            return "0/1";
        }
        long long g = __gcd(abs(denominator), numerator); // 获取最大公约数
        return to_string(denominator / g) + "/" + to_string(numerator / g);
    }
};
int main()
{
    Solution s;
    cout << s.fractionAddition("4/5-1+1/100-23/24") << endl;
    return 0;
}